package net.letcode.core.utils.api;

import com.fasterxml.jackson.annotation.JsonIgnore;
import net.letcode.core.utils.common.DataMap;

import java.util.Map;

public class RequestResult {
    /** 用于服务端判断的错误代码 */
    @JsonIgnore
    private StatusCode code;

    /** 用于客户端判断的错误代码 */
    private RequestStatus status;

    /** 返回数据 */
    private DataMap data;

    public RequestResult() {
        super();
        this.code = StatusCode.Success;
        this.status = new RequestStatus(this.code);
        data = new DataMap();
    }

    public RequestResult(StatusCode statusCode) {
        super();
        this.code = statusCode;
        this.status = new RequestStatus(this.code);
        // 错误代码无需添加数据
        if(statusCode == StatusCode.Success){
            data = new DataMap();
        }
    }

    public StatusCode getCode() {
        return code;
    }

    public void setCode(StatusCode code) {
        this.code = code;
        this.status = new RequestStatus(this.code);
    }

    public RequestStatus getStatus() {
        return status;
    }

    public void setStatus(RequestStatus status) {
        this.status = status;
    }

    public DataMap getData() {
        return data;
    }

    public void setData(DataMap data) {
        this.data = data;
    }

    /**
     * 完整添加一个Map中的所有数据
     * @param map
     */
    public void appendAll(Map<String, Object> map){
        this.data.putAll(map);
    }

    /**
     * 追加单个数据
     * @param key
     * @param value
     */
    public void appendData(String key, Object value){
        this.data.put(key, value);
    }

    /**
     * 移除单个数据
     * @param key
     */
    public void removeData(String key){
        this.data.remove(key);
    }

    /**
     * 清除所有数据
     */
    public void clearData(){
        this.data.clear();
    }

    /**
     * 用于客户端显示的状态信息
     */
    class RequestStatus{
        private Integer code;
        private String message;

        public RequestStatus() {
        }

        public RequestStatus(StatusCode statusCode) {
            this.code = statusCode.getCode();
            this.message = statusCode.getText();
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }
}
